Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
Error del analizador ngStyle angular: token inesperado [, identificador esperado o palabra clave

Necesito poder pasar una cadena dinámica usando fieldName para obtener un atributo dentro del objeto del elemento. Para facilitar la duplicación del problema, he dado un pequeño ejemplo. El problema es que esta sintaxis funciona correctamente con angular 12.2.5 como verifiqué pero no funcionó con angular 12.0.0 o inferior. Simplemente puedo llamar a una función, pero sé que esto no es bueno para el rendimiento. ¿Hay alguna forma alternativa de escribir esta sintaxis para aplicaciones angulares más antiguas?

HTML

 <p [style.background]="item?.[fieldName]"> Some Text </p>

t

 item = { warn: 'yellow', success: 'green', error: 'red' }; fieldName = 'success';

El error se ve así

 Parser Error: Unexpected token [, expected identifier or keyword at column 20 in [{background: item?.[fieldName]}]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Intenta usar el operador ternario ?: :

 <p [style.background]="item[fieldName] ? item[fieldName] : ''"> Some Text </p>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!